package net.tycmc.cty.ctyms.timejob.iems.job;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.cty.ctyms.timejob.common.DateControl;
import net.tycmc.cty.ctyms.timejob.function.monitore.CheckSelfAndReport;
import net.tycmc.cty.ctyms.timejob.function.monitore.ControlHandler;
import net.tycmc.cty.ctyms.timejob.function.monitore.HeartBeat;
import net.tycmc.cty.ctyms.timejob.iems.service.DrivingBehavior2Service;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;

import com.tycmc.functions.util.DynamicReadConfig;
import com.tycmc.functions.util.MapGetter;

public class HandStatDB2InfoJob  {
		private Map<String,Object> param;
		public Logger DB2VclShouDong=Logger.getLogger("DB2VclShouDong");
		public void execute(){
			DB2VclShouDong.info("手动统计档位及车速开始"+ DateControl.getDateTime(0));
			if(ControlHandler.isControl()){
				this.reportHeartBeat();
				this.checkSelf();
			}
		String isbakTable="";
		try {
			isbakTable = DynamicReadConfig.getConfigProperty("config.properties", "DrivingBehavior2");
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DrivingBehavior2Service drivingBehavior2Service = new DrivingBehavior2Service();
		drivingBehavior2Service.initExecuParam();
		List<Map<String, Object>> listData = drivingBehavior2Service.getHandVclInfo();
		if(listData!=null && listData.size()>0){
			String thisWorkDay = "";
			String betime = "";
			String endtime = "";
			String vclId = "";
			String id = "";
			for(Map<String, Object> map : listData){
				id = MapGetter.getString(map, "DB2TI_ID");
				vclId =  MapGetter.getString(map, "DB2TI_VclID");
				betime = MapGetter.getString(map, "DB2TI_BeTime");
				endtime = MapGetter.getString(map, "DB2TI_EndTime");
				List<String> listDate = getDateList(betime, endtime);
				for(String date : listDate){
					thisWorkDay = date;
					System.out.println("手动统计档位车速开始，设备ID："+vclId+",统计日期："+thisWorkDay);
					//变更任务状态为处理中
					drivingBehavior2Service.updateTaskInfo(id,1);
					//统计
					drivingBehavior2Service.startExecu(thisWorkDay,vclId,DB2VclShouDong,isbakTable);
					//统计完成更新此任务状态为已完成
					drivingBehavior2Service.updateTaskInfo(id,2);
					System.out.println("手动统计档位车速结束，设备ID："+vclId+",统计日期："+thisWorkDay);
				}

			}
		}
		DB2VclShouDong.info("手动统计档位及车速开始"+ DateControl.getDateTime(0));
	}
	
	/**
	 * 上报心跳信号
	 **/
	private void reportHeartBeat() {
		HeartBeat hbeat = new HeartBeat();
		this.param = new HashMap<String,Object>();
		String handStatDB2 = "13";
		try {
			handStatDB2 = DynamicReadConfig.getConfigProperty("config.properties", "handStatDB2Key");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		this.param.put("jobID", handStatDB2);
		this.param.put("jobName", "手动统计档位及车速");
		hbeat.produceAndReportHeateBeat(this.param);
	}
	/**
	 * 自检，如果自检未通过则进行报警
	 **/
	private void checkSelf() {
		CheckSelfAndReport csar = new CheckSelfAndReport();
		csar.checkIfFailReport(this.param);
	}
	
	 public List<String> getDateList(String begintime,String endtime){
		    Date dBegin = DateUtil.toDate(begintime, "yyyy-MM-dd");
		    Date dEnd = DateUtil.toDate(endtime, "yyyy-MM-dd");
		     List lDate = new ArrayList();
		   	  lDate.add(begintime);
		   	  Calendar calBegin = Calendar.getInstance();
		   	  // 使用给定的 Date 设置此 Calendar 的时间
		   	  calBegin.setTime(dBegin);
		   	  Calendar calEnd = Calendar.getInstance();
		   	  // 使用给定的 Date 设置此 Calendar 的时间
		   	  calEnd.setTime(dEnd);
		   	  // 测试此日期是否在指定日期之后
		   	  while (dEnd.after(calBegin.getTime()))
		   	  {
		   	   // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
		   	   calBegin.add(Calendar.DAY_OF_MONTH, 1);
		   	   lDate.add(DateUtil.toString(calBegin.getTime(),"yyyy-MM-dd"));
		   	  }
		   	  return lDate;
 }

}
